# Copyright (C) 2009,2010,2011,2012  Marco Restelli
#
# This file is part of:
#   LDGH -- Local Hybridizable Discontinuous Galerkin toolkit
#
# LDGH is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# LDGH is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LDGH. If not, see <http://www.gnu.org/licenses/>.
#
# author: Marco Restelli                   <marco.restelli@gmail.com>


# List of the object files of the present folder
OBJ= complex_eigen.o \
     constants_NSWC.o \
     mod_base.o \
     mod_bcs.o \
     mod_cgdofs.o \
     mod_fe_spaces.o \
     mod_fu_manager.o \
     mod_numquad.o \
     mod_master_el.o \
     mod_grid.o \
     mod_kinds.o \
     mod_linal.o \
     mod_messages.o \
     mod_octave_io.o \
     mod_octave_io_sparse.o \
     mod_octave_io_sympoly.o \
     mod_octave_io_perms.o \
     mod_output_control.o \
     mod_perms.o \
     mod_sparse.o \
     mod_state_vars.o \
     mod_symmon.o \
     mod_sympoly.o \
     mod_umfintf.o \
     mod_utils.o \
     $(MOD_OMP_UTILS) \
     $(UMF4_f77WRAPPER) \
     $(MOD_MPI_UTILS).o \
     $(MOD_UMFPACK).o

# General settings -----------------------------------

PWD:=$(shell pwd)
BUILDDIR:=$(BUILDDIR)/shared

# VPATH includes:
#   PWD for the source files (included by default)
#   BUILDDIR for the .o prerequisites
#   LIBDIR for the final library
VPATH:=$(BUILDDIR) $(LIBDIR)

# clear unfinished targets
.DELETE_ON_ERROR:
# define the phony targets
.PHONY: clean

# check waht to do with OpenMP
ifeq ($(USING_OMP),Yes)
  MOD_OMP_UTILS=mod_omp_utils.o
else
  MOD_OMP_UTILS=
endif

# check waht to do with MPI
ifeq ($(USING_MPI),Yes)
  MOD_MPI_UTILS=mod_mpi_utils
  # remove the unused version of the module
  XXX:=$(shell rm $(BUILDDIR)/mod_mpi_utils_dummy.o 2>/dev/null)
else
  MOD_MPI_UTILS=mod_mpi_utils_dummy
  # we have to be careful how to compile the dummy module
  ifeq ($(FC_BASE),ifort)
    FC_MINIMAL=$(FC) -assume std_mod_proc_name
  else
    FC_MINIMAL=$(FC)
  endif
  # remove the unused version of the module
  XXX:=$(shell rm $(BUILDDIR)/mod_mpi_utils.o 2>/dev/null)
endif

# check waht to do with UMFpack
ifeq ($(USING_UMFPACK),Yes)
  MOD_UMFPACK=mod_umfpack
  UMF4_f77WRAPPER=umf4_f77wrapper.o

  XXX:=$(shell rm $(BUILDDIR)/mod_umfpack_dummy.o 2>/dev/null)
else
  MOD_UMFPACK=mod_umfpack_dummy
  UMF4_f77WRAPPER=

  XXX:=$(shell rm $(BUILDDIR)/mod_umfpack.o \
                  $(BUILDDIR)/umf4_f77wrapper.o 2>/dev/null)
endif

# Main targets ---------------------------------------

libshared.a: $(OBJ)
	-rm $(LIBDIR)/libshared.a
	cd $(BUILDDIR) && $(AR) -r $(LIBDIR)/libshared.a $(OBJ)
	cp $(BUILDDIR)/*.mod $(LIBDIR)/

# General rule
%.o: %.f90
	cd $(BUILDDIR) && $(FC) $(FFLAGS) -c $(PWD)/$<

clean:
	-rm $(BUILDDIR)/*.o $(BUILDDIR)/*.ilo $(BUILDDIR)/*.mod \
          $(LIBDIR)/libshared.a $(LIBDIR)/*.mod

# Specific dependencies: because of USE directives ---

complex_eigen.o: \
  constants_NSWC.o

constants_NSWC.o: \
  mod_kinds.o

mod_base.o: \
  mod_messages.o \
  mod_kinds.o \
  mod_octave_io.o \
  mod_sympoly.o \
  mod_octave_io_sympoly.o \
  mod_numquad.o \
  mod_master_el.o

mod_fe_spaces.o: \
  mod_messages.o \
  mod_kinds.o \
  mod_linal.o \
  mod_sympoly.o \
  mod_master_el.o \
  mod_base.o

mod_cgdofs.o: \
  mod_messages.o \
  mod_kinds.o \
  mod_octave_io.o \
  mod_perms.o \
  mod_master_el.o \
  mod_grid.o \
  mod_bcs.o

mod_fu_manager.o: \
  mod_messages.o

mod_perms.o: \
  mod_messages.o \
  mod_linal.o

mod_numquad.o: \
  mod_messages.o \
  mod_kinds.o \
  mod_fu_manager.o \
  mod_octave_io.o \
  mod_linal.o \
  mod_perms.o

mod_master_el.o: \
  mod_messages.o \
  mod_kinds.o \
  mod_octave_io.o \
  mod_perms.o \
  mod_octave_io_perms.o

mod_grid.o: \
  mod_messages.o \
  mod_kinds.o \
  $(MOD_MPI_UTILS).o \
  mod_fu_manager.o \
  mod_octave_io.o \
  mod_linal.o \
  mod_perms.o \
  mod_master_el.o

mod_bcs.o: \
  mod_messages.o \
  mod_kinds.o \
  $(MOD_MPI_UTILS).o \
  mod_perms.o \
  mod_grid.o

mod_kinds.o: \
  mod_messages.o

mod_linal.o: \
  mod_messages.o \
  mod_kinds.o \
  complex_eigen.o

mod_umfintf.o: \
  mod_messages.o \
  mod_kinds.o \
  mod_sparse.o \
  $(MOD_UMFPACK).o

mod_messages.o: \
  mod_utils.o

mod_omp_utils.o: \
  mod_utils.o

mod_octave_io.o: \
  mod_messages.o \
  mod_kinds.o

mod_octave_io_sparse.o: \
  mod_messages.o \
  mod_kinds.o \
  mod_octave_io.o \
  mod_sparse.o

mod_octave_io_sympoly.o: \
  mod_messages.o \
  mod_kinds.o \
  mod_octave_io.o \
  mod_sympoly.o

mod_octave_io_perms.o: \
  mod_messages.o \
  mod_kinds.o \
  mod_octave_io.o \
  mod_perms.o

mod_output_control.o: \
  mod_messages.o \
  mod_kinds.o

mod_sparse.o: \
  mod_messages.o \
  mod_kinds.o \
  $(MOD_OMP_UTILS)

mod_state_vars.o: \
  mod_messages.o \
  mod_kinds.o

mod_symmon.o: \
  mod_messages.o \
  mod_kinds.o

mod_sympoly.o: \
  mod_messages.o \
  mod_kinds.o \
  mod_symmon.o

mod_umfpack.o mod_umfpack_dummy.o: \
  mod_messages.o \
  mod_kinds.o

mod_mpi_utils.o: \
  mod_messages.o \
  mod_kinds.o

# This module requires many tricks, so we have to switch off
# debugging. Also, we have to hide the .o while compiling the other
# modules.
mod_mpi_utils_dummy.o: mod_mpi_utils_dummy.f90 \
  mod_messages.o \
  mod_kinds.o
	cd $(BUILDDIR) && $(FC_MINIMAL) -O0 -c $(PWD)/$<

# CFLAGS should be set in the main Makefile (it is then exported here)
umf4_f77wrapper.o: umf4_f77wrapper.c
	cd $(BUILDDIR) && \
          $(CC) $(CFLAGS) $(CC_INCLUDE) -c $(PWD)/umf4_f77wrapper.c

